<?php
/**
 * Created by : PhpStorm
 * User: Shane
 * Date: 2023-03-15
 * Time: 11:17
 */

namespace app\admin\model;


use control\CheckData;
use control\MqSearch;
use think\facade\Db;
use think\Model;

class Base extends Model
{

    /**
     * @param string $action
     * @param array $datas
     * @return Model
     * 数据字段验证
     */
    public function validate($action="",$datas=[]){
        $checkRes=CheckData::baseCheck($this->filedRules(),$datas?$datas:request()->param(),$action?$action:request()->action());
        if($checkRes[0]===true){
            return $checkRes[1];
        }else{
            exit(json_encode(["code"=>400,"msg"=>$checkRes[1],"data"=>[]],265));
        }
    }
    /**
     * 搜索验证
     */
    public function search($action="",$datas=[]){
        $action=$action?$action:request()->action();
        return MqSearch::baseCheck($this->searchRule()[$action],$datas?$datas:request()->param());
    }
    /**
     * @param $page
     * @param $num
     * @param array $where
     * @return array
     * 通用列表获取
     */
    public function getList($table,$page, $num, $where = [],$order="id desc",$group='')
    {
        $omenu = Db::name($table)->where($where)->page($page, $num)->order($order)->group($group)->select();
        $count = Db::name($table)->where($where)->count();
        $data = [
            'data' => $omenu,
            'count' => $count
        ];
        return $data;
    }
}
